<?php

require_once 'classes/Pension.php';
require_once 'SCinterface.php';
require_once 'classes/Message.php';

/*
 * Classe de service CRUD pour manipuler les enregistrements de Pensions
 * @author Zikrija
 * @date 04.05.2012
 */
class SCcontratPension {
    
    private $db;

    /**
     *Contructeur créant l'objet pour la connexion à la DB 
     */
    public function __construct() {
        $this->db = new PDO(ACA_DB_DSN, ACA_DB_USER, ACA_DB_PASS, array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''));
        $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    }

    /**
    * Ajoute une Pension
    * @param Pension Pension à ajouter
    */
    public function ajouter($unePensionAAjouter) {
        try{

            $query =  $this->db->prepare("INSERT INTO pension " .
                "(id, dateArrivee, dateDepart, montantParJour, fraisVeto, contrat_id) VALUES(".
                ":id, :dateArrivee, :dateDepart, :montantParJour, :fraisVeto, :contrat_id)");

            $query->execute(array(
                'id'              => $unePensionAAjouter->getId(),
                'dateArrivee'     => $unePensionAAjouter->getDateArrivee(),
                'dateDepart'      => $unePensionAAjouter->getDateDepart(),
                'montantParJour'  => $unePensionAAjouter->getMontantParJour(),
                'fraisVeto'       => $unePensionAAjouter->getFraisVeto(),   
                'contrat_id'      => $unePensionAAjouter->getContrat_id()    
                ));
            $message = new Message("Pension ajoutée");
            $query->closeCursor();
        }
        catch(PDOException $e){
            $message = new Message("Pension non ajoutée: ".$e->getMessage());
        }
        return $unePensionAAjouter;
   }
     
    
    
    /**
     * Methode permettant de modifier une Pension
     * @param Pension Pension à modifier
     * @return boolean 
     */
    public function modifier($unePensionAModifier){
        try{

            $query =  $this->db->prepare("UPDATE pension SET ".
                "dateArrivee = :dateArrivee, dateDepart = :dateDepart , montantParJour = :montantParJour, ".
                "fraisVeto = :fraisVeto, contrat_id = :contrat_id ".
                "WHERE id = :id");

            $query->execute(array(
                'id'              => $unePensionAModifier->getId(),
                'dateArrivee'     => $unePensionAModifier->getDateArrivee(),
                'dateDepart'      => $unePensionAModifier->getDateDepart(),
                'montantParJour'  => $unePensionAModifier->getMontantParJour(),
                'fraisVeto'       => $unePensionAModifier->getFraisVeto(),   
                'contrat_id'      => $unePensionAModifier->getContrat_id()
            ));

            $query->closeCursor();

            $message = new Message("Pension modifiée");

        }
        catch(PDOException $e){
            $message = new Message("Pension non modifiée: ".$e->getMessage());
        }
        return $message;
    }
    /**
     * Methode permettant de supprimer une Pension
     * @param int id de la Pension à supprimer
     * @return boolean 
     */
   /* public function supprimer($idPensionASupprimer){
        try{
            $query = $this->db->prepare("DELETE FROM pension WHERE id = :id");
            $query->execute(array(
                'id'           => $idPensionASupprimer
            ));            
            $query->closeCursor();
            
            $message = new Message("Pension supprimée"); 
        }
        catch(PDOException $e){
            $message = new Message("Pension non supprimée: ". $e->getMessage());
        }
        return $message;
    }
   
    /**
     * 
     * @param Pension de recherche sous forme d'objet Pension
     * @return un tableau d'objet Pension
     */
    public function lister($PensionALister){
        try{
            $sql="SELECT * FROM pension ";
            $sql_Where="WHERE";
            if($PensionALister!=NULL)
            {
                if($PensionALister->getId()!=NULL)
                    $sql_Where.=" id = '".$PensionALister->getId()."' AND ";
                if($PensionALister->getDateArrivee()!=NULL)
                    $sql_Where.=" dateArrivee = '".$PensionALister->getDateArrivee()."' AND ";
                if($PensionALister->getDateDepart()!=NULL)
                    $sql_Where.=" dateDepart = '".$PensionALister->getDateDepart()."' AND ";
                if($PensionALister->getMontantParJour()!=NULL)
                    $sql_Where.=" montantParJour = '".$PensionALister->getMontantParJour()."' AND ";
                if($PensionALister->getFraisVeto()!=NULL)
                    $sql_Where.=" fraisVeto = '".$PensionALister->getFraisVeto()."' AND ";
                if($PensionALister->getContrat_id()!=NULL)
                    $sql_Where.=" contrat_id = '".$PensionALister->getContrat_id()."' AND ";
            }         

            $sql_Where = substr($sql_Where,0,-5);
            //$sql.=$sql_Where." ORDER BY id";
            $sql.=$sql_Where;
            $lesPensionsTrouvees=NULL;
            $unePensionLue=NULL;
            
            //$sql.='INNER JOIN contrat ON contrat_id = '.
            $sql.=" ORDER BY id";
            $result=$this->db->query($sql);
            while($row = $result->fetch())
            {   
                $unePensionLue = new Pension($row['id'], $row['dateArrivee'], $row['dateDepart'], $row['montantParJour'], $row['fraisVeto'], $row['contrat_id']);

                $lesPensionsTrouvees[]=$unePensionLue;
            }                
        }
        catch(PDOException $e)
        {
            print($e->getMessage());
            $lesPensionsTrouvees=NULL;
        }
        return $lesPensionsTrouvees;
    } 
    
     /**
     *
     * @param type $contratBase contient les critères de base du contrat
     * @param type $contratPension contient les critères spécifiques au contrat de pension
     * @return Pension[] une liste de contrat de pension
     */
  /*  public function lister(Contrat $contratBase, $contratPension){
        try{
            $sql="SELECT pension.* FROM pension ";
            if ($contratBase != null) {
                $sql.="INNER JOIN contrat ON pension.contrat_id = contrat.id ";
            }
            $sql_Where="WHERE";
            if($contratPension!=NULL)
            {
                if($contratPension->getId()!=NULL)
                    $sql_Where.=" pension.id = '".$contratPension->getId()."' AND ";
                if($contratPension->getDateArrivee()!=NULL)
                    $sql_Where.=" pension.dateArrivee = '".$contratPension->getDateArrivee()."' AND ";
                if($contratPension->getDateDepart()!=NULL)
                    $sql_Where.=" pension.dateDepart = '".$contratPension->getDateDepart()."' AND ";
                if($contratPension->getMontantParJour()!=NULL)
                    $sql_Where.=" pension.montantParJour = '".$contratPension->getMontantParJour()."' AND ";
                if($contratPension->getFraisVeto()!=NULL)
                    $sql_Where.=" pension.fraisVeto = '".$contratPension->getFraisVeto()."' AND ";
                if($contratPension->getContrat_id()!=NULL)
                    $sql_Where.=" pension.contrat_id = '".$contratPension->getContrat_id()."' AND ";
            }         

            if($contratBase!=NULL)
            {
                if($contratBase->getId()!=NULL)
                    $sql_Where.=" contrat.id = '".$contratBase->getId()."' AND ";
                if($contratBase->getDate() != NULL)
                    $sql_Where.=" contrat.date = '".$contratBase->getDate()."' AND ";
                if($contratBase->getLieu() != NULL)
                    $sql_Where.=" contrat.lieu = '".$contratBase->getLieu()."' AND ";
                if($contratBase->getAnimal_id() != NULL)
                    $sql_Where.=" contrat.animal_id = '".$contratBase->getAnimal_id ()."' AND ";
                if($contratBase->getPersonne_id() != NULL)
                    $sql_Where.=" contrat.personne_id = '".$contratBase->getPersonne_id()."' AND ";
            }            
            
            $sql_Where = substr($sql_Where,0,-5);
            $sql.=$sql_Where." ORDER BY id";

            $lesPensionsTrouvees=NULL;
            $unePensionLue=NULL;

            $result=$this->db->query($sql);
            while($row = $result->fetch())
            {   
                //$unePensionLue = new Pension($row['id'], $row['dateArrivee'], $row['dateDepart'], $row['montantParJour'], $row['fraisVeto'], $row['contrat_id'],
                        //$row['id'], $row['date'], $row['lieu'], $row['animal_id'], $row['personne_id']);
                
                $unePensionLue = new Pension($row['id'], $row['dateArrivee'], $row['dateDepart'], $row['montantParJour'], $row['fraisVeto'], $row['contrat_id'], $row['lieu']);        
                $lesPensionsTrouvees[]=$unePensionLue;
            }                
        }
        catch(PDOException $e)
        {
            print($e->getMessage());
            $lesPensionsTrouvees=NULL;
        }
        return $lesPensionsTrouvees;
    }
    
     */

    /**
     * Méthode pour ajouter un contrat et un contrat Pension à la DB
     * @param Pension $unePensionAAjouter
     * @return pensionAjouter 
     */
  /*  public function ajouter(Pension $unePensionAAjouter) {
        try{
            $query1 = $this->db->prepare("INSERT INTO contrat " .
                    "(id, date, lieu, animal_id, personne_id) VALUES (:id, :date, :lieu, :animal_id, :personne_id)");
            
            $query1->execute(array(
                'id'            => $unePensionAAjouter->getId(),
                'date'          => $unePensionAAjouter->getDate(),
                'lieu'          => $unePensionAAjouter->getLieu(),
                'animal_id'     => $unePensionAAjouter->getAnimal_id(),
                'personne_id'   => $unePensionAAjouter->getPersonne_id()
            ));
            $unePensionAAjouter->setId($this->db->lastInsertId());
            $unePensionAAjouter->setContrat_id($this->db->lastInsertId());
            var_dump($unePensionAAjouter);
            
            $query =  $this->db->prepare("INSERT INTO pension " .
                "(id, dateArrivee, dateDepart, montantParJour, fraisVeto, contrat_id) VALUES (".
                ":id, :dateArrivee, :dateDepart, :montantParJour, :fraisVeto, :contrat_id)");
            
            $query->execute(array(
                'id'              => $unePensionAAjouter->getPid(),
                'dateArrivee'     => $unePensionAAjouter->getDateArrivee(),
                'dateDepart'      => $unePensionAAjouter->getDateDepart(),
                'montantParJour'  => $unePensionAAjouter->getMontantParJour(),
                'fraisVeto'       => $unePensionAAjouter->getFraisVeto(),   
                'contrat_id'      => $unePensionAAjouter->getContrat_id()  
                ));
  
           
            $message = new Message("Pension ajoutée");
            $query->closeCursor();
        }
        catch(PDOException $e){
            $message = new Message("Pension non ajoutée: ".$e->getMessage());
        }
        return $unePensionAAjouter;
   }
   
   /**
     * Methode permettant de modifier un contrat et un contrat de Pension
     * @param Pension unePensionAModifier
     * @return pensionModifie 
     */
  /* public function modifier(Pension $unePensionAModifier){
        try{

            $query1 = $this->db->prepare("UPDATE contrat SET ".
                    "date = :date, lieu = :lieu, animal_id = :animal_id, personne_id = :personne_id ".
                    "WHERE id = :id");
            $query1->execute(array(
                'id'            => $unePensionAModifier->getId(),
                'date'          => $unePensionAModifier->getDate(),
                'lieu'          => $unePensionAModifier->getLieu(),
                'animal_id'     => $unePensionAModifier->getAnimal_id(),
                'personne_id'   => $unePensionAModifier->getPersonne_id()
            ));
            
            
            $query =  $this->db->prepare("UPDATE pension SET ".
                "dateArrivee = :dateArrivee, dateDepart = :dateDepart , montantParJour = :montantParJour, ".
                "fraisVeto = :fraisVeto, contrat_id = :contrat_id ".
                "WHERE id = :id");

            $query->execute(array(
                'id'              => $unePensionAModifier->getPid(),
                'dateArrivee'     => $unePensionAModifier->getDateArrivee(),
                'dateDepart'      => $unePensionAModifier->getDateDepart(),
                'montantParJour'  => $unePensionAModifier->getMontantParJour(),
                'fraisVeto'       => $unePensionAModifier->getFraisVeto(),   
                'contrat_id'      => $unePensionAModifier->getContrat_id()
            ));

            $query->closeCursor();

            $message = new Message("Pension modifiée");

        }
        catch(PDOException $e){
            $message = new Message("Pension non modifiée: ".$e->getMessage());
        }
        return $message;
    }
    
    /**
     * Méthode pour lister les contrats de pension
     * @param type $contratPension contient les critères spécifiques au contrat de pension
     * @return Pension[] une liste de contrat de pension
     */
 /*   public function lister(Pension $contratPension){
        try{
            $sql="SELECT DISTINCT * FROM pension INNER JOIN contrat ON pension.contrat_id = contrat.id ";

            $sql_Where="WHERE";
            if($contratPension!=NULL)
            {
                if($contratPension->getPid()!=NULL)
                    $sql_Where.=" pension.id = '".$contratPension->getPid()."' AND ";
                if($contratPension->getDateArrivee()!=NULL)
                    $sql_Where.=" pension.dateArrivee = '".$contratPension->getDateArrivee()."' AND ";
                if($contratPension->getDateDepart()!=NULL)
                    $sql_Where.=" pension.dateDepart = '".$contratPension->getDateDepart()."' AND ";
                if($contratPension->getMontantParJour()!=NULL)
                    $sql_Where.=" pension.montantParJour = '".$contratPension->getMontantParJour()."' AND ";
                if($contratPension->getFraisVeto()!=NULL)
                    $sql_Where.=" pension.fraisVeto = '".$contratPension->getFraisVeto()."' AND ";
                if($contratPension->getContrat_id()!=NULL)
                    $sql_Where.=" pension.contrat_id = '".$contratPension->getContrat_id()."' AND ";
                if($contratPension->getId()!=NULL)
                    $sql_Where.=" contrat.id = '".$contratPension->getId()."' AND ";
                if($contratPension->getDate() != NULL)
                    $sql_Where.=" contrat.date = '".$contratPension->getDate()."' AND ";
                if($contratPension->getLieu() != NULL)
                    $sql_Where.=" contrat.lieu = '".$contratPension->getLieu()."' AND ";
                if($contratPension->getAnimal_id() != NULL)
                    $sql_Where.=" contrat.animal_id = '".$contratPension->getAnimal_id ()."' AND ";
                if($contratPension->getPersonne_id() != NULL)
                    $sql_Where.=" contrat.personne_id = '".$contratPension->getPersonne_id()."' AND ";
            }                   
            
            $sql_Where = substr($sql_Where,0,-5);
            $sql.=$sql_Where." ORDER BY id";

            $lesPensionsTrouvees=NULL;
            $unePensionLue=NULL;

            $result=$this->db->query($sql);
            //$result->setFetchMode(PDO::FETCH_OBJ);
            while($row = $result->fetch())
            {   
                $unePensionLue = new Pension($row['id'], $row['dateArrivee'], $row['dateDepart'], $row['montantParJour'], $row['fraisVeto'], $row['contrat_id'],
                        $row['id'], $row['date'], $row['lieu'], $row['animal_id'], $row['personne_id']);
                        
                /*$unePensionLue = new Pension($row, $row['dateArrivee'], $row['dateDepart'], $row['montantParJour'], $row['fraisVeto'], $row['contrat_id'],
                        $row['contrat.id'], $row['contrat.date'], $row['contrat.lieu'], $row['contrat.animal_id'], $row['contrat.persone_id']);        

                echo $row->id;
$lesPensionsTrouvees[]=$unePensionLue;
            }
        //}
        }
        catch(PDOException $e)
        {
            print($e->getMessage());
            $lesPensionsTrouvees=NULL;
        }
        return $lesPensionsTrouvees;
    }
    */
}

?>
